


console.log("get follow item start");
chrome.runtime.onMessage.addListener(async (msg, sender) => {
  console.log(" get ozon detail msg ok ")
  console.log(msg)
  var sku = msg['data']['sku_id'];
  if(!sku){
        console.log("no sku!! no data!!")
        return;
  }
  getItemStatus(msg);
})


function getItemStatus(msg){

    var url = "https://www.ozon.ru/product/"+msg.data.sku_id;
    var sku = msg.data.sku_id;
    var sendtime = msg['data']['sendtime'];
    console.log("getItemStatus:"+url)
    $.ajax({
      url: url,
      method: 'GET',
      dataType: 'html',
      success: function(htmlContent) {
         console.log(htmlContent);
         var status = 0;

        if(htmlContent.includes('location.replace')){
            status = -1;
        }else{
            const dom = $($.parseHTML(htmlContent));
            //完全删除不可恢复
            dom.find('div[id^="state-error-"]').each(function() {
                var body = $(this).attr("data-state");
                var data_state_obj = JSON.parse(body);
                if(null!=data_state_obj && data_state_obj.title == 'Такой страницы не существует'){
                        status = -2;
                }
            });

            var title =  dom.filter('title').text();;
            console.log(title)
            if(null!=title){
                 status = 1;
            }
        }

        /*getSendTime(sku,htmlContent);
        if(sendtime==1){

        }*/

        console.log("status:"+status);
        var type="getfollowitemstatus";
        chrome.runtime.sendMessage({ data: {type ,sku,status}, close: false });

      },
      error: function(xhr, status, error) {
        console.log('Ajax error: ', error);
        if (xhr.status === 403 && status === "error") {
            var type="ozonapi403";
            chrome.runtime.sendMessage({ data: {type}, close: false });
            console.log("遇到了403错误");
             startNextQ();
            //中断
        }
      }
    });
}


function getSendTime(sku,htmlContent){

    const regex = /window\.__NUXT__\.state='(.*?)'/;
    const match = htmlContent.match(regex);
    console.log(match);
    if(match && match[1]){
        let replacedStr = match[1].replace(/\\"/g, '"');

        var data = JSON.parse(replacedStr);

        var asyncData = "";
        var widgetStateId = "";
        console.log(data);
        if(data['layout']){
                data['layout'].forEach((item1) => {

                    if(!item1.placeholders){return;}
                    item1.placeholders.forEach((item2) => {
                             if(!item2.widgets){return;}
                             item2.widgets.forEach((item3) => {
                                    if(!item3.placeholders){return;}
                                    item3.placeholders.forEach((item4) => {
                                        if(!item4.widgets){return;}
                                        item4.widgets.forEach((item5) => {
                                            if(item5.component=='webDelivery'  && item5.stateId && item5.stateId.startsWith("webDelivery-")){
                                                    console.log(item5)
                                                    asyncData = item5.asyncData;
                                                    widgetStateId = item5.stateId;
                                            }
                                            if(!item5.placeholders){return;}
                                            item5.placeholders.forEach((item6) => {
                                                    if(!item6.widgets){return;}
                                                    item6.widgets.forEach((item7) => {
                                                        if(item7.component=='webDelivery'  && item7.stateId  && item7.stateId.startsWith("webDelivery-")){
                                                                console.log(item7)
                                                                asyncData = item7.asyncData;
                                                                widgetStateId = item7.stateId;
                                                        }
                                                         if(!item7.placeholders){return;}
                                                         item7.placeholders.forEach((item8) => {
                                                            if(!item8.widgets){return;}
                                                            item8.widgets.forEach((item9) => {
                                                                if(item9.component=='webDelivery'  && item9.stateId && item9.stateId.startsWith("webDelivery-") ){
                                                                        console.log(item9)
                                                                        asyncData = item9.asyncData;
                                                                        widgetStateId = item9.stateId;
                                                                }

                                                    });

                                            });

                                                    });

                                            });

                                        });
                                    });
                             });
                    });

            });
         }

        console.log(asyncData);
        console.log(widgetStateId);
    }

       if(!asyncData){
            return
       }

        var url ="https://www.ozon.ru/api/composer-api.bx/widget/json/v2?widgetStateId="+widgetStateId;
        var asydata = {};
        asydata.asyncData = asyncData;

         $.ajax({
          type: "POST",
          url: url,
          data: JSON.stringify(asydata),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(response) {
            console.log(response);

          }
        });

}

